package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type AssetCategoryAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetCategoryAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetCategoryAddLogic {
	return &AssetCategoryAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建资产分类
// AssetCategoryAdd 创建资产分类
func (l *AssetCategoryAddLogic) AssetCategoryAdd(in *property_service.AssetCategory) (*property_service.ResponseId, error) {
	// 获取资产分类模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetCategory
	// 创建一个新的资产分类模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.AssetCategory{
		ParentID:              in.ParentID,              // 父级ID
		ParentCode:            in.ParentCode,            //父级编码
		ParentLs:              in.ParentLs,              // 父级列表
		CategoryCode:          in.CategoryCode,          // 分类编号
		CustomCodeFlag:        in.CustomCodeFlag,        // 编号规则
		Name:                  in.Name,                  // 分类名称
		PinyinCode:            in.PinyinCode,            // 拼音简码
		DepreciationMonths:    in.DepreciationMonths,    // 折旧年限
		ManufacturerID:        in.ManufacturerID,        // 生产厂商ID
		ManufacturerName:      in.ManufacturerName,      // 生产厂商名称
		FinancialCategoryID:   in.FinancialCategoryID,   // 财务分类id
		FinancialCategoryName: in.FinancialCategoryName, // 财务分类名称
		BrandID:               in.BrandID,               // 品牌id
		BrandName:             in.BrandName,             // 品牌名称
		Status:                in.Status,                // 状态
		StatusName:            in.StatusName,            // 状态名称
		UnitPrice:             in.UnitPrice,             // 单价
		Unit:                  in.Unit,                  // 单位
		IsComponent:           in.IsComponent,           // 部件标志
		NeedMaintenance:       in.NeedMaintenance,       // 预防性维护
		Remark:                in.Remark,                // 备注
		StockID:               in.StockID,               // 仓库ID
		TenantID:              in.TenantID,              // 租户ID
		CreatedAtuser:         in.CreatedAtuser,         // 创建用户
		AssetTypeID:           in.AssetTypeID,           //资产类型id
		AssetTypeName:         in.AssetTypeName,         //资产类型名称
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 ContractMainAddLogic 结构体中添加辅助方法
func (l *AssetCategoryAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
